package server.model.evaluationModel;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import server.db.DBDriver;
import server.db.DBFactory;
import server.entity.Evaluation;
import server.util.InfoUtils.Info;
import server.util.InfoUtils.InfoEnum;
import server.util.InfoUtils.InfoUtil;
import server.util.MyUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EvaluationModel {
    //添加评价
    public Info addEvaluation(Evaluation e) {
        String sql = "insert into evaluation (good_id,user_id,star,message,pic,date) values (?,?,?,?,?,now())";

        if (!MyUtils.NOTNULL(e.getGoodId(), e.getUserId(), e.getStar())) {
            return InfoUtil.getInfo(InfoEnum.PARAMS_NULL);
        }

        ArrayList<Object> params = new ArrayList<>();
        params.add(e.getGoodId());
        params.add(e.getUserId());
        params.add(e.getStar());
        params.add(e.getMessage());
        params.add(e.getPic());

        int line = DBFactory.getDBDriver().update(sql, params.toArray());
        if (line > 0) {
            return InfoUtil.getInfo(InfoEnum.SUCCESS);
        } else {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }
    }

    public Info getEvaluationByGoodId(int goodId) {
        String sql = "select * from evaluation where good_id = ? order by date desc";

        List<Evaluation> evaluations = DBFactory.getDBDriver().query(sql, new BeanListHandler<>(Evaluation.class, DBDriver.getProcess()), goodId);

        if (evaluations == null) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        } else if (evaluations.size() == 0) {
            return InfoUtil.getInfo(1, "该商品没有评价");
        } else {
            return InfoUtil.getInfo(evaluations);
        }
    }

    public Info getEvaluationByUserId(int userId) {
        String sql = "select * from evaluation where user_id = ? order by date desc";

        List<Evaluation> evaluations = DBFactory.getDBDriver().query(sql, new BeanListHandler<>(Evaluation.class, DBDriver.getProcess()), userId);

        if (evaluations == null) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        } else if (evaluations.size() == 0) {
            return InfoUtil.getInfo(1, "您还没有评价过");
        } else {
            return InfoUtil.getInfo(evaluations);
        }
    }

    //获取star平均数
    public Info getAverage(int goodId) {
        String sql = "select avg(star) from evaluation where good_id = " + goodId;
        String average = DBFactory.getDBDriver().queryOne(sql);
        if (!MyUtils.NOTNULL(average)) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }
        Double ans = Double.parseDouble(average);
        return InfoUtil.getInfo(ans);
    }

    //获取star的统计数据
    public Info getStatistic(int goodId) {
        ArrayList<Integer> data = new ArrayList<>(Arrays.asList(0,0,0,0,0));
        String sql = "select star from evaluation where good_id = ?";
        List<Evaluation> evaluations = DBFactory.getDBDriver().query(sql, new BeanListHandler<>(Evaluation.class, DBDriver.getProcess()), goodId);

        if (evaluations == null) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        } else if (evaluations.size() == 0) {
            return InfoUtil.getInfo(1, "您还没有评价过");
        }

        long sum = 0, count = 0;
        for (Evaluation e : evaluations) {
            int star = e.getStar();
            sum += star;
            count++;
            data.set(star - 1, data.get(star - 1) + 1);
        }
        Double average = (double) (sum / count);
        return InfoUtil.getInfo(0,average.toString(),data);
    }
}
